﻿# encoding=utf-8
# author: walker
# date:
# summary:

import os
import time


import facade
from bs4 import BeautifulSoup
from xjlibrary.our_file_dir import BaseDir

StartTime = time.time()

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -3)
sPath = BaseDir.get_new_path(TopPath, "download", "cnki_cg", "download")
DIR_TIME_DETAILS = os.path.join(sPath, 'year_html')  # abstractdetails fulltextdetails
# HtmlYearRoot = os.path.join(curPath, 'year_html')
class_dict = {'A': '基础科学', 'B': '工程科技Ⅰ辑', 'C': '工程科技Ⅱ辑', 'D': '农业科技', 'E': '医药卫生科技', 'F': '哲学与人文科学', 'G': '社会科学Ⅰ辑',
              'H': '社会科学Ⅱ辑', 'I': '信息科技', 'J': '经济与管理科学'}

Proxies = {
    'http': '127.0.0.1:8087',
    # 'http':'162.105.138.192:8092',
    'https': '127.0.0.1:8087'  # key是指目标网站的协议
}
mysqlutils = facade.MysqlUtiles(BaseDir.get_new_path(curPath, "db.ini"), "db", facade.get_streamlogger())


#
# # 数据库链接
# def MajorDbConnect():
#     return MySqlDbConnect(curPath, "db.ini")


def ClearDb():
    print('-----------清空列表--------------------')
    # conn = MajorDbConnect()
    deleteSql = 'delete from class_year'
    # ExeSqlToDB(deleteSql, conn)
    mysqlutils.ExeSqlToDB(deleteSql)


# 解析年份目录页，生成年份统计数据，并入库. 输入参数 classes: 成果类型
#													class_name: 成果类型中文名


def ProcAllYear(classes, class_name):
    iDict = {}
    YearPath = os.path.join(DIR_TIME_DETAILS, classes + '.htm')
    print(YearPath)
    soup = BeautifulSoup(open(YearPath, 'r', encoding='utf-8'), 'lxml')
    alist = soup.find_all('a')
    years = list()
    print('a:')
    for a in alist:
        years.append(a.string)
    print(len(years))

    numlist = soup.find_all('span', style='color:#999;')
    nums = list()
    print('span:')
    for num in numlist:
        nums.append(int(num.string.replace('(', '').replace(')', '')))
    print(len(nums))

    for i in range(len(years)):
        iDict[years[i]] = nums[i]

    # conn = MajorDbConnect()

    for year in sorted(iDict.keys()):
        print('ProcOneYear ' + str(year) + ', ' + str(iDict[year]) + '...')
        sql = "INSERT INTO class_year(classes, years, class_name, num) VALUES('%s', %d, '%s', %d)" % (
            classes, int(year), class_name, iDict[year])

        mysqlutils.ExeSqlToDB(sql)


# 调用浏览器（淘汰）
# def GetYear():
#
#     chromedriver = r"C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chromedriver.exe"
#     chromeOptions = webdriver.ChromeOptions()
#     prefs = {"profile.managed_default_content_settings.images": 2}
#     chromeOptions.add_experimental_option("prefs", prefs)
#     browser = webdriver.Chrome(chromedriver, chrome_options=chromeOptions)
#     browser.delete_all_cookies()
#
#     url = 'http://kns.cnki.net/kns/brief/result.aspx?dbPrefix=CISD'
#     browser.get(url)
#     time.sleep(2)
#
#     url = r"http://kns.cnki.net/kns/request/SearchHandler.ashx?action=&NaviCode={classes}&catalogName=SCSD_CCSCLS&ua=1.25&PageName=ASP.brief_result_aspx&DbPrefix=CISD&DbCatalog=%e6%a0%87%e5%87%86%e6%95%b0%e6%8d%ae%e6%80%bb%e5%ba%93&ConfigFile=CISD.xml&db_opt=CISD&his=0".format(
#         classes=classes)
#     # url =
#     # "http://kns.cnki.net/kns/request/SearchHandler.ashx?action=&NaviCode='{classes}'&catalogName=SCSD_CCSCLS&ua=1.25&PageName=ASP.brief_result_aspx&DbPrefix=CISD&DbCatalog=%e6%a0%87%e5%87%86%e6%95%b0%e6%8d%ae%e6%80%bb%e5%ba%93&ConfigFile=CISD.xml&db_opt=CISD&his=0".format(classes
#     # = classes)
#     browser.get(url)
#     time.sleep(1)
#
#     url = 'http://kns.cnki.net/kns/group/doGroupLeft.aspx?action=1&Param=ASP.brief_result_aspx%23CISD/%u5E74/%u5E74%2Ccount%28*%29/%u5E74/%28%u5E74%2C%27date%27%29%23%u5E74%24desc/1000000%24/-/40/40000/ButtonView&cid=0&clayer=0'
#     browser.get(url)
#     time.sleep(1)


def ReadNaviCodeFromDb():
    sql = 'SELECT subid,subname FROM subclass'
    return mysqlutils.SelectFromDB(sql)


# 用作工作串
def DoWork():
    ClearDb()
    taskList = ReadNaviCodeFromDb()
    if taskList:
        for task in taskList:
            print(task[0] + ':' + task[1])
            ProcAllYear(task[0], task[1])

    # ProcAllYear()
    # GetYear()
    print('Game Over!!!')
    print('Time total:' + repr(time.time() - StartTime) + '\n')


def main(logger=None):
    # os.system('PAUSE')
    # raw_input('Press any key and Enter to continue ~!')
    # print(len(class_dict))
    ClearDb()

    taskList = ReadNaviCodeFromDb()
    if taskList:
        for task in taskList:
            print(task[0] + ':' + task[1])
            ProcAllYear(task[0], task[1])

    # ProcAllYear()
    # GetYear()
    print('Game Over!!!')
    print('Time total:' + repr(time.time() - StartTime) + '\n')


if __name__ == '__main__':
    main()
